package org.dingyom.user.controller;

import org.dingyom.basics.util.AjaxResult;
import org.dingyom.user.dto.SmsCodeDto;
import org.dingyom.user.service.IVerificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//验证码
@RestController
@RequestMapping("/verification")
public class VerificationController {
    @Autowired
    IVerificationService verificationService;
    @GetMapping("/img/{key}")
    public AjaxResult getImgCodeByKey(@PathVariable("key") String key){
        try {
            String base64Date = verificationService.getImgCodeByKey(key);
            return AjaxResult.me().setData(base64Date);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取验证码失败！"+e.getMessage());
        }
    }

    @PostMapping("/sms/register")
    public AjaxResult register(@RequestBody SmsCodeDto dto){
        try {
            return verificationService.sendSmsCode(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发送验证码失败！"+e.getMessage());
        }
    }

    @PostMapping("/sms/loginBySms")
    public AjaxResult loginBySms(@RequestBody SmsCodeDto dto){
        try {
            return verificationService.sendSmsCode2login(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发送验证码失败！"+e.getMessage());
        }
    }

}
